/**
 * 
 */
package se.izenman.outilsServer;

import android.util.Log;
import java.net.URI;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;

/**
 * @author lusta 
 *
 */
public class CallRPC {
	private static XMLRPCClient client;
	private static URI uri;

        public static int connect = 0;
        public static Thread monThread;
	/**
	 * Méthode permettant l'execution des appels au server pour l'application VLC
	 * @param action Action à executer pour vlc
	 */
	public static int ExecuteVLC(final EActions action, boolean etat) throws InterruptedException {
            uri = URI.create(InfoServer.getUrlServer());
            client = new XMLRPCClient(uri);
            connect = 0;
            
            monThread = new Thread(){

                @Override
                public void run()  {
                    String connectionString = EActions._SERVERNAME.getValue() + action.getValue();
                    try {
                        client.call(connectionString);
                        connect=0;
                    } catch (XMLRPCException ex) {
                        Logger.getLogger(CallRPC.class.getName()).log(Level.SEVERE, null, ex);
                        connect=1;
                    }
                    Log.i("Call-RPC", connectionString);
                } 
            };

            monThread.start();

            Timer timer = new Timer();

            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    if(monThread.isAlive()){
                        monThread.interrupt();
                        connect=1;
                    }
                }
            }, 2000);
            
            if(etat){
               Thread.sleep(3000);
            }
            return connect; 
	}
	
	public static int ExecutePADMove(EActions action, int deplacementX, int deplacementY){
		uri = URI.create(InfoServer.getUrlServer());
		client = new XMLRPCClient(uri);
		
		try {
            	        
            String connectionString = EActions._SERVERNAME.getValue() + action.getValue();
			client.call(connectionString, deplacementX, deplacementY);
			Log.i("Call-RPC", connectionString);
			connect = 0;
                                          
		} catch (XMLRPCException e) {
			e.printStackTrace();
		}
		
		return 0;
	}
}
